﻿/*
' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Configuration;
using System.Text;

namespace DotNetNuke.Modules.dnnwhois
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from dnnwhoisModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : dnnwhoisModuleBase, IActionable
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }


        public class resultswhois
        {
            public string stringwhois;
        }

        public string evalvoid(System.Text.RegularExpressions.Match m)
        {




            if (m.Groups[1].Value != "" && m.Groups[2].Value != "")
            {
                whoisresolvip(m.Groups[1].Value, m.Groups[2].Value);
            }



            return m.ToString();
        }


        public void whoisresolvip(string ip, string port)
        {


            resultswhois rrr1 = new resultswhois();
            try
            {
                TcpClient objTCPC = new TcpClient(ip, Convert.ToInt32(port));
                string strDomain = Regex.Replace(TextBox1.Text, "(http://)(www.)", "") + "\r\n";
                string strDomain1 = Regex.Replace(strDomain, "www.", "");


                byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain1);

                Stream objStream = objTCPC.GetStream();
                objStream.Write(arrDomain, 0, strDomain1.Length);
                StreamReader objSR = new StreamReader(objTCPC.GetStream(), Encoding.UTF8);





                rrr1.stringwhois = "<b>" + Request.Form["DomainName"] + "</b><br><br>" + Regex.Replace(objSR.ReadToEnd(), "\n", "<br>");

                objTCPC.Close();
            }
            catch (Exception ex)
            {



                
                lblDialog.Text += ex.ToString();
            }



            
            lblDialog.Text += rrr1.stringwhois;
        }

        public void whoisresolv(string service)
        {
            resultswhois rrr1 = new resultswhois();
            MatchEvaluator ipeval = new MatchEvaluator(evalvoid);

            try
            {
                TcpClient objTCPC = new TcpClient(service, 43);
                string strDomain = Regex.Replace(TextBox1.Text, "(http://)(www.)", "") + "\r\n";
                string strDomain1 = Regex.Replace(strDomain, "www.", "");
 
                byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain1);

                Stream objStream = objTCPC.GetStream();
                objStream.Write(arrDomain, 0, strDomain1.Length);
                StreamReader objSR = new StreamReader(objTCPC.GetStream(), Encoding.UTF8);





                rrr1.stringwhois = "<b>" + Request.Form["DomainName"] + "</b><br><br>" + Regex.Replace(objSR.ReadToEnd(), "\n", "<br>");

                

                
                objTCPC.Close();
            }
            catch (Exception ex)
            {

                
                lblDialog.Text += ex.ToString();

            }

            if (rrr1.stringwhois.IndexOf("Early registration") > -1 || rrr1.stringwhois.IndexOf("Not allocated") > -1 || rrr1.stringwhois.IndexOf("ReferralServer") > -1 || rrr1.stringwhois.IndexOf("No match") > -1 || rrr1.stringwhois.IndexOf("No entries found") > -1 || rrr1.stringwhois.IndexOf("FAILURE TO LOCATE A RECORD") > -1 || rrr1.stringwhois.IndexOf("No Match") > -1 || rrr1.stringwhois.IndexOf("NOT FOUND") > -1)
            {

            }
            else
            {
                lblDialog.Text += rrr1.stringwhois;
          
            }


        }




        protected void Button1_Click(object sender, EventArgs e)
        {

            lblDialog.Text = "";


            whoisresolv("whois.ripn.net");
            whoisresolv("whois.nic.biz");
            whoisresolv("whois.crsnic.net");
            whoisresolv("whois.educause.net");
            whoisresolv("whois.publicinterestregistry.net");
            whoisresolv("whois.afilias.net");
            whoisresolv("whois.internic.net");
            whoisresolv("whois.arin.net");
            whoisresolv("whois.apnic.net");
            whoisresolv("whois.lacnic.net");
           

            StringBuilder sb = new StringBuilder();
            sb.Append("$(function() { ");
            sb.Append(" $('#dialog').dialog({");
            sb.Append("title: 'Whois',");
            sb.Append("dialogClass: 'dnnFormPopup',");
            sb.Append("width: 500");
            sb.Append(" });");
            sb.Append("});");
            Page.ClientScript.RegisterStartupScript(typeof(Page), "myscript", sb.ToString(), true);





        }


        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                    {
                        {
                            GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), "", "", "",
                            EditUrl(), false, SecurityAccessLevel.Edit, true, false
                        }
                    };
                return actions;
            }
        }
    }
}